// pages/my/myInfo_address_add/myInfo_address_add.js
const API = require('../../../API/api.js');
const app = getApp();
const area = require('../../../utils/area.js')
import Dialog from '../../../utils/vant-weapp/dialog/dialog';

Page({
    onChangeName(e) {
        this.setData({
            name: e.detail
        })
        console.log(this.data.name)
    },
    onChangeAddress(e) {
        this.setData({
            addressInfo: e.detail
        })
        console.log(this.data.addressInfo)
    },
    onChangeTel(e) {
        this.setData({
            mobile: e.detail
        })
        console.log(this.data.mobile)
    },
    onChange({detail}) {
        // 需要手动对 checked 状态进行更新
        this.setData({checked: detail});
    },
    popup() {
        this.setData({
            show: true
        })
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    onCofirm(e) {
        console.log(e.detail.values);
        let addressArr = e.detail.values;
        let temArr = [];
        for (let i = 0; i < addressArr.length; i++) {
            temArr.push(addressArr[i].name)
        }
        console.log(temArr)
        this.setData({
            show: false,
            areaInfo: temArr,
            province: temArr[0],
            city: temArr[1],
            district: temArr[2]
        });

    },
    onCancel() {
        this.setData({
            show: false
        })
    },
    addressAdd() {
        let province = this.data.areaInfo[0]
        let city = this.data.areaInfo[1]
        let district = this.data.areaInfo[2]
        let isMain;
        if (this.data.checked == true) {
            isMain = 1
        } else {
            isMain = 0
        }
        API.addAddress({
            userId: wx.getStorageSync('userId'),
            name: this.data.name,
            mobile: this.data.mobile,
            province: province,
            city: city,
            district: district,
            detail: this.data.addressInfo,
            isMain: isMain
        }).then((res) => {
            if (res.code == 0) {
                return Dialog.alert({
                    message: res.msg
                }).then(() => {
                    // on close
                });
            } else {
                wx.redirectTo({
                    url: '../../my/myInfo_address/myInfo_address',
                })
            }
            console.log(res)
        })
    },
    /**
     * 页面的初始数据
     */
    data: {
        checked: true,
        areaList: area.default,
        show: false,
        name: '',
        addressInfo: '',
        mobile: '',
        addressId: '',
        areaInfo: '',
        province: '',
        city: '',
        district: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            this.setData({
                addressId: options.id
            })
            API.getAddressInfo({
                userId: wx.getStorageSync('userId'),
                addressId: options.id
            }).then((res) => {
                console.log(res)
                let isMain;
                if (this.data.checked == 1) {
                    isMain = true
                } else {
                    isMain = false
                }
                this.setData({
                    checked: isMain,
                    name: res.data.name,
                    addressInfo: res.data.detail,
                    mobile: res.data.mobile,
                    province: res.data.province,
                    city: res.data.city,
                    district: res.data.district
                })
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
})
